package com.zmn.oms.services.impl.pay;

import com.zmn.common.utils.date.DateUtil;
import com.zmn.oms.common.annotation.OrderPayChangeSync;
import com.zmn.oms.common.annotation.ZsNormalOrderWorkLog;
import com.zmn.oms.common.constant.OrderLogConsts;
import com.zmn.oms.model.dto.work.orderpay.CancelPayDTO;
import com.zmn.oms.model.entity.pay.OrderPay;
import com.zmn.oms.persistence.interfaces.pay.OrderPayDao;
import com.zmn.oms.services.interfaces.pay.OrderPayService;
import com.zmn.pay.common.constant.PayConsts;
import io.shardingsphere.api.HintManager;
import io.shardingsphere.core.keygen.DefaultKeyGenerator;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Optional;

/**
 * @author ：SongWeidong
 * @date ：2021/3/31 17:09
 * @description：支付明细
 */
@Slf4j
@Service
public class OrderPayServiceImpl implements OrderPayService {

	@Resource
	private OrderPayDao orderPayDao;
	@Autowired
	private DefaultKeyGenerator defaultKeyGenerator;

	@Override
	public OrderPay findOrderPayByKey(Long orderId, Long orderPayId) {
		return orderPayDao.findOrderPayByKey(orderId, orderPayId);
	}

	@Override
	public OrderPay findOrderPayByKeySrcMaster(Long orderId, Long orderPayId) {
		try (HintManager hintManager = HintManager.getInstance()) {
			hintManager.setMasterRouteOnly();
			return orderPayDao.findOrderPayByKey(orderId, orderPayId);
		}
	}

	@Override
	public OrderPay findAllOrderPayByKeySrcMaster(Long orderId, Long orderPayId) {
		try (HintManager hintManager = HintManager.getInstance()) {
			hintManager.setMasterRouteOnly();
			return orderPayDao.findAllOrderPayByKey(orderId, orderPayId);
		}
	}

	@Override
	public int countByWorkIdAndAmountType(Long orderId, Long workId, Integer amountType, Integer tradeType) {
		try (HintManager hintManager = HintManager.getInstance()) {
			hintManager.setMasterRouteOnly();
			return orderPayDao.countByWorkIdAndAmountType(orderId, workId, amountType, tradeType);
		}
	}

	@Override
	public int countByOrderIdAndWorkIdSrcMaster(Long orderId, Long workId) {
		try (HintManager hintManager = HintManager.getInstance()) {
			hintManager.setMasterRouteOnly();
			return orderPayDao.countByOrderIdAndWorkId(orderId, workId);
		}
	}

	@Override
	public List<OrderPay> listOrderPayByOrderId(Long orderId) {
		try (HintManager hintManager = HintManager.getInstance()) {
			hintManager.setMasterRouteOnly();
			return orderPayDao.listOrderPayByOrderId(orderId);
		}
	}

	@Override
	public List<OrderPay> listEsOrderPayByOrderIdList(List<Long> orderIdList) {
		try (HintManager hintManager = HintManager.getInstance()) {
			hintManager.setMasterRouteOnly();
			return orderPayDao.listEsOrderPayByOrderIdList(orderIdList);
		}
	}

	@Override
	public List<OrderPay> listOrderPayByOrderIdList(List<Long> orderIdList) {
		try (HintManager hintManager = HintManager.getInstance()) {
			hintManager.setMasterRouteOnly();
			return orderPayDao.listOrderPayByOrderIdList(orderIdList);
		}
	}

	@Override
	public List<OrderPay> listOrderPayByOrderIdAndWorkIdMasterSrc(Long orderId, Long workId) {
		try (HintManager hintManager = HintManager.getInstance()) {
			hintManager.setMasterRouteOnly();
			return orderPayDao.listOrderPayByOrderIdAndWorkId(orderId,workId);
		}
	}

	@Override
	public List<OrderPay> listOrderPayByOrderIdAndWorkIdAndAmountTypeMasterSrc(Long orderId, Long orderPayId,Integer amountType) {
		try (HintManager hintManager = HintManager.getInstance()) {
			hintManager.setMasterRouteOnly();
			return orderPayDao.listOrderPayByOrderIdAndWorkIdAndAmountType(orderId, orderPayId, amountType);
		}
	}
	@Override
	public List<OrderPay> listOrderPayByOrderIdAndWorkId(Long orderId, Long workId) {
		return orderPayDao.listOrderPayByOrderIdAndWorkId(orderId,workId);
	}

	@Override
	public List<OrderPay> listAllOrderPayByOrderIdAndWorkId(Long orderId, Long workId) {
		return orderPayDao.listAllOrderPayByOrderIdAndWorkId(orderId,workId);
	}

    @Override
    public List<OrderPay> listOrderPayByOrderIdAndWorkIdsAndTradeTypeSrc(Long orderId, List<Long> workIds, Integer tradeType) {
		try (HintManager hintManager = HintManager.getInstance()) {
			hintManager.setMasterRouteOnly();
			return orderPayDao.listOrderPayByOrderIdAndWorkIdsAndTradeType(orderId,workIds,tradeType);
		}
    }

    @Override
    public List<OrderPay> listOrderPayByOrderIdAndWorkIdsAndTradeType(Long orderId, List<Long> workIds, Integer tradeType) {
        return orderPayDao.listOrderPayByOrderIdAndWorkIdsAndTradeType(orderId,workIds,tradeType);
    }

	@Override
	public OrderPay getOrderWorkRefundPay(Long orderId, Long workId) {
		return orderPayDao.getOrderWorkRefundPay(orderId, workId, PayConsts.PAY_TRADE_TYPE_REFUND);
	}

	@Override
	@OrderPayChangeSync
    public void insertOrderPay(OrderPay orderPay) {
		orderPay.setUpdateTime(Optional.ofNullable(orderPay.getUpdateTime()).orElse(DateUtil.getNow()));
        orderPayDao.insertOrderPay(orderPay);
    }

	@Override
	@OrderPayChangeSync
	public int updateByPrimaryKeySelective(OrderPay orderPay) {
		orderPay.setUpdateTime(Optional.ofNullable(orderPay.getUpdateTime()).orElse(DateUtil.getNow()));
		return orderPayDao.updateByPrimaryKeySelective(orderPay);
	}

	@Override
	@OrderPayChangeSync
	public int deleteByWorkIdAndAmountType(OrderPay orderPay) {
		return orderPayDao.deleteByWorkIdAndAmountType(orderPay.getOrderId(), orderPay.getWorkId(),
				orderPay.getAmountType(), orderPay.getTradeType());
	}

	@Override
	@OrderPayChangeSync
	public int deleteByKey(OrderPay orderPay) {
		return orderPayDao.deleteByKey(orderPay.getOrderId(), orderPay.getOrderPayId());
	}

	@Override
	@Transactional
	@OrderPayChangeSync
	@ZsNormalOrderWorkLog(type = OrderLogConsts.ORDER_LOG_TYPE_CANCEL_PAY, beforeProceed = false)
	public int deleteByKeyRecordLog(CancelPayDTO cancelPayDTO) {
		return orderPayDao.deleteByKey(cancelPayDTO.getOrderId(), cancelPayDTO.getOrderPayId());
	}
}
